package com.woniu.inlet.web.controller;

import com.woniu.exception.NoticeException;
import com.woniu.inlet.web.command.AddNoticeCommand;
import com.woniu.inlet.web.fo.FixedDayVo;
import com.woniu.inlet.web.fo.FixedMsgVo;
import com.woniu.outlet.mysql.po.Notice;
import com.woniu.response.ResponseResult;
import com.woniu.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@Slf4j
@Api("个人中心通知相关测试")
@CrossOrigin
public class NoticeController {
    @Autowired
    private NoticeService noticeService;


    @GetMapping("/user/notice/get")
    @ApiOperation("个人中心查询通知的测试方法")
    public ResponseResult getNotices(Integer userId) {
        try {
            List<Notice> notices = noticeService.getByUserId(userId);
            return ResponseResult.ok(notices);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("通知系统维护中，请稍后再试。。。");
        }
    }


    @GetMapping("/user/notice/update")
    @ApiOperation("修改通知状态")
    public ResponseResult updateNotice(Integer id) {
        try {
            noticeService.updateById(id);
            return ResponseResult.ok("通知状态修改成功");
        } catch (Exception e) {
            if (e instanceof NoticeException)
                return new ResponseResult(200, e.getMessage());
            else
                return ResponseResult.fail("通知系统维护中。。。");
        }
    }


    /*1.基金，黄金的定投买入时订单调用，定投由系统定时任务自动生成订单
     * 根据flag判断定投订单是否成功，成功扣钱，存定投成功的消息，存交易记录，
     * 反之不扣钱，存定投失败的消息
     */
    @PostMapping("/user/notice")
    public boolean notice(
            @RequestParam("flag") String flag,
            @RequestParam("userId") Integer userId,
            @RequestParam("money") BigDecimal money,
            @RequestBody FixedMsgVo fixedMsgVo) {
        try {
            AddNoticeCommand command = new AddNoticeCommand(flag, userId, money, fixedMsgVo);
            command.excute();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
